﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.dz.ht.common;
using System.Text.RegularExpressions;
using com.dz.ht.common;
using com.dz.ht.components;
namespace com.dz.ht.ui
{
    public partial class FrmChpaterContent : Form
    {
        string m_book_id = string.Empty;
        string m_book_name = string.Empty;
        string m_site_encoding = string.Empty;
        string m_site_domain = string.Empty;
        string m_chapter_list_url = string.Empty;
        string m_chapter_url = string.Empty;
        string m_reg_content = string.Empty;
        string m_reg_chapter=string.Empty;
        int m_link_type = 2;
        public FrmChpaterContent()
        {
            InitializeComponent();
        }

        public FrmChpaterContent(string bookId,string bookName,string siteDomain, string siteEncoding,string chapterListUrl, string chapterUrl,string regChapter)
        {
            m_book_id = bookId;
            m_book_name = bookName;
            m_site_domain = siteDomain;
            m_site_encoding = siteEncoding;
            m_chapter_url = chapterUrl;
            m_chapter_list_url = chapterListUrl;
            m_reg_chapter=regChapter;
            if (m_chapter_url.StartsWith("http"))
            {
                m_link_type = 1;
            }
            else if (m_chapter_url.StartsWith("/"))
            {
                m_link_type = 0;
                m_chapter_url = siteDomain + m_chapter_url;
            }
            else
            {
                if (chapterListUrl.EndsWith("/"))
                {
                    m_chapter_url = chapterListUrl + m_chapter_url;
                }
                else
                {
                    m_chapter_url = chapterListUrl.Substring(0, chapterListUrl.LastIndexOf("/") + 1) + m_chapter_url;
                }
            }
              
         
         
            InitializeComponent();
            txtDomain.Text = m_site_domain;
        }

        private void btnSure_Click(object sender, EventArgs e)
        {

            Encoding t_encoding = Encoding.GetEncoding(m_site_encoding);
            string t_html = NetSiteCatchManager.ReadUrl(m_chapter_url, t_encoding);


            if (!string.IsNullOrEmpty(t_html))
            {
                m_reg_content = txtRegContent.Text.Trim();
                Match t_mc = Regex.Match(t_html, m_reg_content, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_mc == null || t_mc.Groups.Count <= 0)
                {
                    MessageBox.Show("正则错误!");
                    return;
                }
                txtContent.Text = t_mc.Groups[1].Value.ToString();
            }
            else
            {
                MessageBox.Show("检查网络!");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SearchSiteInfo t_site=new SearchSiteInfo();
            long t_book_id=0;
            if(!long.TryParse(m_book_id,out t_book_id))
                t_book_id=0;
            t_site.BookId=t_book_id;
            t_site.ChapterUrl=m_chapter_list_url;
            t_site.ForbidWords=txtForbidWords.Text.Trim();
            t_site.LinkType=m_link_type;
            t_site.RegChapter=m_reg_content;
            t_site.RegChapterList=m_reg_chapter;
            t_site.SiteDomain=m_site_domain;
            t_site.SiteEncoding=m_site_encoding;
            try
            {
                DataManger db = new DataManger();
                int ret = db.UpdateBookSearchSite(t_site);
                if (ret == Consts.Success)
                {
                    MessageBox.Show("保存成功");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }

            }
            catch(Exception ex)
            {
                LogHelper.Error("保存搜索站点失败"+ex.ToString());
                MessageBox.Show("保存失败");
            }
        }
    }
}
